package main.java.com.amanda.suafa;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import main.java.com.amanda.utils.Graph;

import java.util.Scanner;


/**
 * false未完成
 * @author amanda
 * @Description 符号图的数据类型
 * 允许用例用字符串代替数字索引来表示图中的顶点
 */
public class SymbolGraph {
    private ST<String, Integer> st; // 符号名 -> 索引
    private String[] keys;          // 索引 -> 符号名
    private Graph G;                // 图
    public SymbolGraph(String stream, String sp) {
        st = new ST<>();
        In in = new In(stream);
        while (!in.isEmpty()) {
            String[] a = in.readLine().split(sp);
            for (int i = 0; i < a.length; i++) {
                if (!st.contains(a[i]))
                    st.put(a[i], st.size());
            }
            keys = new String[st.size()];
            for (String name : st.keys()) {
                keys[st.get(name)] = name;
                G = new Graph(st.size());
                while (in.hasNextLine()) {
                    String[] b = in.readLine().split(sp);
                    int v = st.get(a[0]);
                    for (int i = 1; i < b.length; i++)
                        G.addEdge(v, st.get(a[i]));
                }
            }
        }
    }
    public boolean contains(String s) {
        return st.contains(s);
    }
    public int index(String x) {
        return st.get(x);
    }
    public String name(int v) {
        return keys[v];
    }
    public Graph G() {
        return G;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String filename  = scanner.nextLine();
        String delimiter = scanner.nextLine();

        SymbolGraph sg = new SymbolGraph(filename, delimiter);
        Graph graph = sg.G();

        while (StdIn.hasNextLine()) {
            String source = StdIn.readLine();
            if (sg.contains(source)) {
                int s = sg.index(source);
                for (int v : graph.adj(s)) {
                    StdOut.println("   " + sg.name(v));
                }
            }
            else {
                StdOut.println("input not contain '" + source + "'");
            }
        }
    }
}
/*
JFK MCO ORD DEN ORD HOU DFW PHX JFK ATL ORD DFW ORD PHX ATL HOU DEN PHX PHX LAX JFK ORD DEN LAS DFN HOU ORD ATL LAS LAX ATL MCO HOU MCO LAS PHX
 */
